package domain;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import domain.gbshaasrode.spreadsheet.GBSHaasrodeSpreadSheetService;
import domain.gbshaasrode.spreadsheet.GBSOpvangSpreadSheet;
import domain.gbshaasrode.spreadsheet.GBSOpvangWorkSheetLeerlingen;

public class School {
	private ArrayList<Leerling> leerlingen;

	public School(){
		leerlingen = new ArrayList<Leerling>();
	}

	public void voegToe(Leerling leerling){
		leerlingen.add(leerling);
	}

	public ArrayList<Leerling> getLeerlingen(){
		return leerlingen;
	}

	public void maakCodes(String folder) throws DomainException{
		QRGenerator qrGenerator = new QRGenerator();
		for(Leerling l: getLeerlingen()){
			if(!l.isQrCodeGegenereerd()){
				int nr = createEntryInDb(l);
				String qrString = ""+nr+System.getProperty("line.separator")+l.toString();
				BitMatrix matrix = qrGenerator.generate(qrString, 200, 200);
				String filePath = folder + "QR"+l.getAchternaam()+l.getVoornaam()+".png";
				File qrfile = new File(filePath);
				try {
					MatrixToImageConfig config = 
							new MatrixToImageConfig(0xFFDE1E09, 0x00FFFFFF);
					MatrixToImageWriter.writeToFile(matrix, "png", qrfile, config);
					l.setQrCodeGegenereerd(true);
				} catch (IOException e) {
					throw new DomainException("cannot write to png",e);
				}
				createCard(folder, l,qrfile);
				System.out.println("Kaart gegenereerd voor: ");
				System.out.println(l.toString());
			}
		}		
	}
	
	private int createEntryInDb(Leerling l)  throws DomainException{
		int llnr = -1;
		GBSHaasrodeSpreadSheetService service;
		service = new GBSHaasrodeSpreadSheetService();
		GBSOpvangSpreadSheet ss = new GBSOpvangSpreadSheet(service);
		GBSOpvangWorkSheetLeerlingen ll = new GBSOpvangWorkSheetLeerlingen(service, ss);
		llnr = ll.addRow(l.getAchternaam(),l.getVoornaam(), l.getStraatnaam(), l.getHuisnummer(), l.getPostcode(), l.getStad());
		return llnr;
	}

	private void createCard(String folder, Leerling l, File qrfile) throws DomainException{
		try {
			InputStream llkaartsrc = getClass().getResourceAsStream("/llKaart.JPG");
			//URL llkaartsrc = ((new File("res\\llKaart.JPG")).toURI()).toURL();
			BufferedImage llKaartHaasrode = ImageIO.read(llkaartsrc);
			URL llQRcodesrc = qrfile.toURI().toURL();
			BufferedImage llQRcode = ImageIO.read(llQRcodesrc);
			BufferedImage bi = new BufferedImage(482, 303, BufferedImage.TYPE_INT_ARGB);
			Graphics big = bi.getGraphics();
			big.drawImage(llKaartHaasrode, 0, 0, null);
			big.setColor(new Color(255,255,255,255));
			big.drawImage(llQRcode, 280, 100, null);
			big.setColor(Color.black);
			InputStream is = getClass().getResourceAsStream("/comicbd.ttf");//new FileInputStream("res\\comicbd.ttf");
			try {
				Font comic = Font.createFont(Font.TRUETYPE_FONT, is);
				comic = comic.deriveFont(Font.BOLD, 24f);
				big.setFont(comic);
			} catch (FontFormatException e) {
				throw new DomainException("font cannot be created",e);
			}
			String naam = l.getVoornaam() + " " + l.getAchternaam();
			char[] charsNaam = naam.toCharArray();
			big.drawChars(charsNaam, 0, charsNaam.length, 20, 40);
			String straatnr = l.getStraatnaam() + " " + l.getHuisnummer();
			char[] charsStraat = straatnr.toCharArray();
			big.drawChars(charsStraat, 0, charsStraat.length, 20, 80);
			String stad = l.getPostcode() + " " + l.getStad();
			char[] charsStad = stad.toCharArray();
			big.drawChars(charsStad, 0, charsStad.length, 20, 120);
			String filePath = folder + "Card"+l.getAchternaam()+l.getVoornaam()+".png";
			File cardfile = new File(filePath);
			if (!ImageIO.write(bi, "png", cardfile)) {
				throw new DomainException("Could not write an image of format " + "jpg" + " to " + cardfile);
			}
		} catch (MalformedURLException e) {
			throw new DomainException("cannot read card template",e);
		} catch (IOException e) {
			throw new DomainException("cannot write",e);
		}

	}
}
